//
//  TitleViewController.swift
//  SwiftScrollTitleAndView
//
//  Created by 李萍 on 16/2/25.
//  Copyright © 2016年 李萍. All rights reserved.
//

import UIKit

class TitleViewController: UIViewController, UIScrollViewDelegate {
    var titleScrollView: UIScrollView!
    var subViewScrollView: UIScrollView!
    
    var titles: [String]! //滚动标题数组
    var controllers: [UIViewController]!    //滚动视图数组
    var hasNav: Bool! //是否有导航栏
    
    var hasTabbar: Bool! //是否有工具栏
    var titlesHeight: CGFloat!  //标题高度
    var buttonWidth: CGFloat!   //按钮宽度
    var footHeight: CGFloat!    //滚动条高度
    
    var buttonBackgroundColor: UIColor! //标题按钮的背景色
    var buttonUnSelectedColor: UIColor! //标题按钮未选中时的颜色
    var buttonSelectedColor: UIColor! //标题按钮选中时的颜色
    
    var buttons: [UILabel] = [] //用于存储标题按钮数组
    var foots: [UIView] = [] //用于存储滚动条数组
    var sizeWidth: CGFloat! //屏幕宽度
    var sizeHeight: CGFloat!    //屏幕高度
    
    var titlesCount: Int!   //标题数组个数
    var controllerCount: Int!
    
    init(titles: [String], controllers: [UIViewController], hasNav: Bool, hasTabbar:Bool, titlesHeight: CGFloat, buttonWidth: CGFloat, footHeight: CGFloat) {
        self.titles = titles
        self.controllers = controllers
        self.hasNav = hasNav
        self.hasTabbar = hasTabbar
        
        self.titlesHeight = titlesHeight
        self.buttonWidth = buttonWidth
        self.footHeight = footHeight
        
        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.userInteractionEnabled = true
        
        sizeWidth = UIScreen.mainScreen().bounds.size.width
        sizeHeight = UIScreen.mainScreen().bounds.size.height
        titlesCount = titles.count
        controllerCount = controllers.count
        
        if (hasNav == true) {
            if (hasTabbar == true) {
                sizeHeight = sizeHeight - 104
            } else {
                sizeHeight = sizeHeight - 64
            }
        } else {
            if (hasTabbar == true) {
                sizeHeight = sizeHeight - 64
            } else {
                sizeHeight = sizeHeight - 20
            }
        }
        
        if buttonWidth > 0 { } else {
            buttonWidth = sizeWidth/(CGFloat(titles.count))
        }
        
        self.setInitForScrollView()
        self.setContentForTitleScrollView()
        self.setContentForSubViewScrollView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
    
    //初始化两个滚动视图
    func setInitForScrollView() {
        titleScrollView = UIScrollView()
        if (hasNav != nil) {
            titleScrollView.frame = CGRect.init(x: 0, y: 64, width: sizeWidth, height: titlesHeight)
        } else {
            titleScrollView.frame = CGRect.init(x: 0, y: 20, width: sizeWidth, height: titlesHeight)
        }
        titleScrollView.backgroundColor = buttonBackgroundColor
        titleScrollView.pagingEnabled = true
        titleScrollView.bounces = false
        titleScrollView.showsHorizontalScrollIndicator = false
        titleScrollView.showsVerticalScrollIndicator = false
        titleScrollView.delegate = self
        if buttonWidth > 0 {
            titleScrollView.contentSize = CGSizeMake(CGFloat(buttonWidth * CGFloat(titlesCount)), titlesHeight)
        } else {
            titleScrollView.contentSize = CGSizeMake(sizeWidth, titlesHeight)
        }
        self.view.addSubview(titleScrollView)
        
        
        subViewScrollView = UIScrollView()
        if (hasNav != nil) {
            subViewScrollView.frame = CGRect.init(x: 0, y: 64 + CGFloat(titlesHeight), width: sizeWidth, height: sizeHeight)
        } else {
            subViewScrollView.frame = CGRect.init(x: 0, y: 20 + CGFloat(titlesHeight), width: sizeWidth, height: sizeHeight)
        }
        subViewScrollView.contentSize = CGSizeMake(CGFloat(sizeWidth * CGFloat(controllerCount)), sizeHeight)
        subViewScrollView.pagingEnabled = true
        subViewScrollView.delegate = self
        subViewScrollView.showsHorizontalScrollIndicator = false
        subViewScrollView.bounces = false
        self.view.addSubview(subViewScrollView)
    }
    
    //标题栏添加按钮
    func setContentForTitleScrollView() {
        if titles.count > 0 {
            for var i = 0; i < titles.count; i++ {
                let titleButton = UILabel.init(frame: CGRectMake(buttonWidth * CGFloat(i), 0, buttonWidth, titlesHeight - CGFloat(footHeight)))
                titleButton.text = titles[i]
                titleButton.textAlignment = NSTextAlignment.Center
                titleButton.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "clickTitle:"))
                titleButton.userInteractionEnabled = true
                titleButton.backgroundColor = buttonBackgroundColor
                titleButton.tag = i+1;
                buttons.append(titleButton)
                titleScrollView.addSubview(titleButton)
                
                
                let footView = UIView.init(frame: CGRectMake(buttonWidth * CGFloat(i), titlesHeight - CGFloat(footHeight), buttonWidth, footHeight))
                footView.backgroundColor = buttonBackgroundColor
                footView.tag = i+1000
                foots.append(footView)
                titleScrollView.addSubview(footView)
                
                if i == 0 {
                    titleButton.textColor = buttonSelectedColor
                    footView.backgroundColor = buttonSelectedColor
                } else {
                    titleButton.textColor = buttonUnSelectedColor
                    footView.backgroundColor = buttonBackgroundColor
                }
                
            }
        }
    }
    
    //标题按钮功能
    func clickTitle(recognizer: UITapGestureRecognizer) {
        let label = recognizer.view as! UILabel
        let buttonIndex = label.tag

        for subLabel in buttons {
            if subLabel.tag == buttonIndex {
                subLabel.textColor = buttonSelectedColor
            } else {
                subLabel.textColor = buttonUnSelectedColor
            }
        }
        
        for footView in foots {
            if footView.tag == buttonIndex+999 {
                footView.backgroundColor = buttonSelectedColor
            } else {
                footView.backgroundColor = buttonBackgroundColor
            }
        }
        
        //滚动对应的视图
        subViewScrollView.scrollRectToVisible(CGRectMake(sizeWidth*CGFloat(buttonIndex-1), 0, sizeWidth, sizeHeight - CGFloat(titlesHeight)), animated: true)
    }
    
    //滚动页面
    func setContentForSubViewScrollView() {
        
        if controllers.count > 0 {
            for var i = 0; i < controllers.count; i++ {
                let subViewController = controllers[i] as UIViewController
                subViewController.view.frame = CGRectMake(sizeWidth * CGFloat(i), 0, sizeWidth, sizeHeight - CGFloat(titlesHeight))
                subViewController.view.autoresizingMask = [UIViewAutoresizing.FlexibleHeight, UIViewAutoresizing.FlexibleWidth]
                
                subViewScrollView.addSubview(subViewController.view)
                self.addChildViewController(subViewController)
            }
        }
    }
    //滚动页面带动标题滚动
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        if scrollView == subViewScrollView {
            let viewIndexTag = NSInteger(scrollView.contentOffset.x/sizeWidth)
            titleScrollView.scrollRectToVisible(CGRectMake(sizeWidth * CGFloat(viewIndexTag-2), 0, sizeWidth, titlesHeight), animated: true)
            
            for subLabel in buttons {
                if subLabel.tag == (viewIndexTag+1) {
                    subLabel.textColor = buttonSelectedColor
                } else {
                    subLabel.textColor = buttonUnSelectedColor
                }
            }
            
            for footView in foots {
                if footView.tag == viewIndexTag+1000 {
                    footView.backgroundColor = buttonSelectedColor
                } else {
                    footView.backgroundColor = buttonBackgroundColor
                }
            }

        }
    }

}
